import { provide, ref } from "vue";
import { TableField, TableUrl } from "@/models/TableSetting";
import api from "@/utils";

export default function BaseSubmitForm(
  props: {
    url: TableUrl;
    tableFieldList: Array<TableField>;
    submitForm: Record<string, unknown>;
  },
  emit: {
    (
      e: "data-format",
      columnName: string,
      cellValue: string,
      callback: (value: string) => void
    ): void;
    (
      e: "set-form-init-value",
      formType: string,
      row: Record<string, unknown>
    ): void;
    (e: "submit-valid", callback: (value: boolean) => void): void;
  },
  getRecord: () => void
) {
  const submitFormDialogVisible = ref(false);
  provide("submitFormDialogVisible", submitFormDialogVisible);

  const changeSubmitFormDialogVisible = () => {
    submitFormDialogVisible.value = !submitFormDialogVisible.value;
  };
  provide("changeSubmitFormDialogVisible", changeSubmitFormDialogVisible);

  // 表单功能类型(新增还是编辑)
  const submitFunctionType = ref();
  provide("submitFunctionType", submitFunctionType);

  // 设置表单功能类型的值
  const setSubmitFunctionType = (type: string) => {
    submitFunctionType.value = type;
  };
  provide("setSubmitFunctionType", setSubmitFunctionType);

  // 设置不同类型的表单的不同的初始值
  const setFormInitValue = (row: Record<string, unknown>) => {
    emit(
      "set-form-init-value",
      submitFunctionType.value,
      Object.assign({}, row)
    );
  };
  provide("setFormInitValue", setFormInitValue);

  // 提交事件
  const submit = async () => {
    const isPassValid = ref(false);
    await emit("submit-valid", (value: boolean) => {
      isPassValid.value = value;
    });
    if (isPassValid.value) {
      sendSubmitForm().then(() => {
        console.log("校验成功 " + isPassValid.value);
      });
    } else {
      console.log("校验失败 " + isPassValid.value);
    }
  };
  provide("submit", submit);

  const batchSubmit = async (data: Record<string, unknown>[]) => {
    console.log("进入批量提交");
    console.log(data);
    await api.http
      .postJson(props.url?.modifyingBatchUrl, data)
      .then((res) => {
        console.log(res);
        getRecord();
      })
      .catch((err) => {
        console.log(err);
      });
  };
  provide("batchSubmit", batchSubmit);

  //向后端发送新增或编辑请求
  const sendSubmitForm = async () => {
    switch (submitFunctionType.value) {
      case "insert":
        await api.http
          .postJson(props.url?.modifyingSingleUrl, props.submitForm)
          .then((res) => {
            console.log(res);
            getRecord();
          })
          .catch((err) => {
            console.log(err);
          });
        break;
      case "update":
        await api.http
          .putJson(
            props.url?.modifyingSingleUrl + "/" + props.submitForm.id,
            props.submitForm
          )
          .then((res) => {
            console.log(res);
            getRecord();
          });
        break;
    }
  };
  return {
    submitFormDialogVisible,
    changeSubmitFormDialogVisible,
    setSubmitFunctionType,
    setFormInitValue,
  };
}
